package org.fordprefect.struts2jscalendarplugin.components;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.TextField;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * <!-- START SNIPPET: javadoc -->
 * 
 * Renders datepicker element.</p>
 * 
 * the struts2jscalendar-plugin uses <a
 * href="http://www.dynarch.com/projects/calendar/old/">jscalendar 1.0</a> </p>
 * 
 * Following a reference for the format parameter (copied from jscalendar
 * documentation):
 * <table border=0>
 * <tr>
 * <td valign=top ></td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%a</tt></td>
 * <td valign=top >abbreviated weekday name</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%A</tt></td>
 * <td valign=top >full weekday name</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%b</tt></td>
 * <td valign=top >abbreviated month name</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%B</tt></td>
 * <td valign=top >full month name</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%C</tt></td>
 * <td valign=top >century number</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%d</tt></td>
 * <td valign=top >the day of the month ( 00 .. 31 )</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%e</tt></td>
 * <td valign=top >the day of the month ( 0 .. 31 )</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%H</tt></td>
 * <td valign=top >hour ( 00 .. 23 )</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%I</tt></td>
 * <td valign=top >hour ( 01 .. 12 )</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%j</tt></td>
 * <td valign=top >day of the year ( 000 .. 366 )</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%k</tt></td>
 * <td valign=top >hour ( 0 .. 23 )</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%l</tt></td>
 * <td valign=top >hour ( 1 .. 12 )</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%m</tt></td>
 * <td valign=top >month ( 01 .. 12 )</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%M</tt></td>
 * <td valign=top >minute ( 00 .. 59 )</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%n</tt></td>
 * <td valign=top >a newline character</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%p</tt></td>
 * <td valign=top >``PM'' or ``AM''</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%P</tt></td>
 * <td valign=top >``pm'' or ``am''</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%S</tt></td>
 * <td valign=top >second ( 00 .. 59 )</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%s</tt></td>
 * <td valign=top >number of seconds since Epoch (since Jan 01 1970 00:00:00
 * UTC)</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%t</tt></td>
 * <td valign=top >a tab character</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%U, %W, %V</tt></td>
 * <td valign=top >the week number</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%u</tt></td>
 * <td valign=top >the day of the week ( 1 .. 7, 1 = MON )</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%w</tt></td>
 * <td valign=top >the day of the week ( 0 .. 6, 0 = SUN )</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%y</tt></td>
 * <td valign=top >year without the century ( 00 .. 99 )</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%Y</tt></td>
 * <td valign=top >year including the century ( ex. 1979 )</td>
 * </tr>
 * <tr>
 * <td valign=top ><tt>%%</tt></td>
 * <td valign=top >a literal <tt>%</tt> character</td>
 * </tr>
 * </table>
 * <p>
 * 
 * <!-- END SNIPPET: javadoc -->
 * 
 * <p/>
 * <b>Examples</b>
 * 
 * <pre>
 * <!-- START SNIPPET: expl1 -->
 * Date in application's locale format:
 *     &lt;jscalendar:jscalendar name="order.date" id="order.date" /&gt;
 * Date with custom date format, including time:
 *     &lt;jscalendar:jscalendar name="invoice.date" id="invoice.date" format="%d. %b %Y %H:%M" showstime="true" /&gt;
 * <!-- END SNIPPET: expl1 -->
 * </pre>
 * 
 * @author Patrick Lightbody
 * @author Rene Gielen
 * @version $Revision: 2541 $
 * @ww.tag name="datepicker" tld-body-content="JSP"
 *         tld-tag-class="org.fordprefect.struts2.views.jsp.ui.JSCalendarTag"
 *         description="Render datepicker"
 * @since 2.2
 */
@StrutsTag(name = "jscalendar", tldTagClass = "org.apache.struts2.views.jsp.ui.JSCalendarTag", description = "Renders JSCalendar datepicker")
public class JSCalendar extends TextField {

	final public static String TEMPLATE = "jscalendar";

	protected String language;
	protected String format;
	protected String showstime;
	protected String singleclick;
	protected String flat;
	protected String flatCallback;

	public JSCalendar(ValueStack stack, HttpServletRequest request,
			HttpServletResponse response) {
		super(stack, request, response);
	}

	@Override
	protected String getDefaultTemplate() {
		return TEMPLATE;
	}

	@Override
	public void evaluateParams() {
		super.evaluateParams();

		if (format != null) {
			addParameter("format", findString(format));
		}

		if (showstime != null) {
			addParameter("showstime", findString(showstime));
		}

		if (singleclick != null) {
			addParameter("singleclick", findString(singleclick));
		}
		if (flat != null) {
			addParameter("flat", findString(flat));
		}
		if (flatCallback != null) {
			addParameter("flatCallback", findString(flatCallback));
		}

	}

	/**
	 * The format to use for date field.
	 * 
	 * @s.tagattribute required="false" type="String"
	 *                 default="Dateformat specified by language preset (%Y/%m/%d for en)"
	 */
	@StrutsTagAttribute(description = "The format to use for date field.", required = false, defaultValue = "Dateformat specified by language preset (%Y/%m/%d for en)", type = "String")
	public void setFormat(String format) {
		this.format = format;
	}

	/**
	 * Whether time selector is to be shown. Valid values are &quot;true&quot;,
	 * &quot;false&quot;, &quot;24&quot; and &quot;12&quot;.
	 * 
	 * @s.tagattribute required="false" type="String" default="false"
	 */
	@StrutsTagAttribute(description = "Whether time selector is to be shown. Valid values are &quot;true&quot;, &quot;false&quot;, &quot;24&quot; and &quot;12&quot;.", required = false, defaultValue = "false", type = "String")
	public void setShowstime(String showstime) {
		this.showstime = showstime;
	}

	/**
	 * Whether to use selected value after single or double click.
	 * 
	 * @s.tagattribute required="false" type="Boolean" default="true"
	 */
	@StrutsTagAttribute(description = "Whether to use selected value after single or double click.", required = false, defaultValue = "true", type = "Boolean")
	public void setSingleclick(String singleclick) {
		this.singleclick = singleclick;
	}

	public void setFlat(String flat) {
		this.flat = flat;
	}

	public void setFlatCallback(String flatCallback) {
		this.flatCallback = flatCallback;
	}
}
